package com.wxf.web;

import com.wxf.bean.Student;
import com.wxf.service.StudentService;
import com.wxf.service.impl.StudentServiceImpl;
import com.wxf.util.StudentStateEnum;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(urlPatterns = "/Educational/student/addStudentInfo")
public class AddStudentInfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1-获取请求参数
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gradeId = req.getParameter("gradeId");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        // 2-封装成student对象
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setgId(Integer.parseInt(gradeId));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        student.setState(StudentStateEnum.Normal.state);
        student.setRegdate(new Date());

        // 3-后台处理
        StudentService studentService = new StudentServiceImpl();
        int influenceRow = studentService.addStudentInfo(student);

        // 4-响应
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        if(influenceRow==0){
            pw.println("<script>alert('添加失败');location.href='/Educational/student/getGradeInfo'</script>");
        }else{
            pw.println("<script>alert('添加成功');location.href='/Educational/student/GetStudentList'</script>");
        }
    }
}
